<template>
  <!-- 事业部列表 -->
  <div style="height:520px">
    <FilterTable
      ref="table"
      height="450"
      :dataSource="dataSource"
      :columns="columns"
      :isSelectColumn="false"
      :isToperInfo="false"
      :fetchapi="fetchParams"
      :params="flterValue"
      :datakey="'records'"
      :formType="value.type"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_projram_commonForm_infoCause_01"
    >
      <template slot="controls" v-if="value.type!=='show'">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="openDialog(null,'add')"
        >添加事业部</el-button>
      </template>
    </FilterTable>
    <!-- 经销商弹窗 -->
    <BaseDialog
      title="选择事业部"
      :visible.sync="visibleDialog"
      width="800px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <addMadal ref="infoCauseDialogA" v-model="initModal" @close="closeDialog" />
    </BaseDialog>
  </div>
</template>

<script>
import addMadal from './infoCauseDialog';
import { dictionary } from '@/utils/dictMixin';
import { saveProjectOrg } from '@/api/cms-api/projectOperating/program/push';
import { getShiYeDept } from '@/api/cms-api/projectOperating/program/push'; //事业部
export default {
  mixins: [dictionary],
  components: {
    addMadal
  },
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      activityDialogData: {},
      visibleDialog: false,
      initModal: {},
      formValue: {},
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: null, //ipa 接口
      arr: {},
      dataSource: []
    };
  },
  mounted() {
    // debugger
    // console.log("infoCarse2:",this.value) //获取到行id
    //请求接口进行赋值，自己写
    //如果传入参数不为空，则说明是修改进入，
    // 测试事业部
    if (this.value != null && (this.value.type == 'edit' || this.value.type == 'show')) {
      //  console.log("infoCarse1:",this.value);
      this.init();
    }
  },
  methods: {
    //修改时，初始化页面数据

    async init() {
      //  console.log("infoCause3:initForm:",this.initForm,"this.arr=",this.arr,"this.value",this.value)

      this.arr.projectProgramId = this.value.rowData['projectProgramId'];
      // this.arr.deptName = this.value.rowData["deptName"];
      this.arr.currentPage = 1;
      this.arr.pageNum = 1;
      this.arr.isDeleted = 0;
      this.arr.pageSize = 10; //dataSource不支持分页
      const res = await getShiYeDept(this.arr);
      // console.log(res,"事业部:res")

      if (res.resultCode === 200) {
        // this.$notify.success({title: '提示信息',message:'getData成功!'});
        //  PROJECT_START_DATE PROJECT_END_DATE
        var result = res.data.records;

        // console.log(" result2:=",result)

        this.dataSource = result; //res.data.records;
        this.formValue = this.dataSource;
      } else {
        this.$notify.error({ title: '提示信息', message: res.errMsg || 'getShiYeDept失败!' });
      }
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.$index, 'del')}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        { title: '事业部代码', dataIndex: 'orgCode', sorter: true },
        { title: '事业部名称', dataIndex: 'orgName', sorter: true }
      ];
    },
    openDialog(row, type) {
      this.visibleDialog = true;
      this.visible = true;
      const pram = {
        type: 'add',
        rowData: null
      };

      this.initModal = pram;
    },

    editHandle(index, type) {
      //集合的加减判断放到server端.
      this.dataSource.splice(index, 1);

      // this.formValue.splice(index,1);
    },
    closeDialog(data) {
      if (data.data != undefined) {
        if (0 == data.data.length) {
          this.$notify.warning({ title: '提示信息', message: '请选择数据!' });
          return;
        }

        var isSame  = false;
        data.data.forEach(element => {
          for (var i = 0; i < this.dataSource.length; i++) {
            if (this.dataSource[i].orgId == element.orgId) {
              isSame = true;
             // this.$notify.warning({ title: '提示信息', message: '事业部重复不可添加!' });
              return;
            }
          }

          this.dataSource.push(Object.assign({}, element));
          this.dataSource.dataMark=false
        });

        if (isSame) {
          this.$notify.warning({ title: '提示信息', message: '事业部重复不可添加!' });
        }
        //返回数据逻辑自己写
        this.formValue = this.dataSource;

        //返回数据逻辑自己写
        // this.visible = false;
        this.visibleDialog = data.visible; //关闭弹窗
        if (data.type === 'refresh') {
          // 重新加载
          this.flterValue = { ...this.flterValue };
        }
      } else {
        this.visibleDialog = false; //关闭弹窗
      }
    }
  }
};
</script>
